package cn.tedu.csmall.business.controller;

import cn.tedu.csmall.business.service.IBusinessService;
import cn.tedu.csmall.commons.restful.JsonResult;
import cn.tedu.csmall.commons.restful.ResponseCode;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/business")
@Api(tags = "业务触发模块")
public class BusinessController {
    @Autowired
    private IBusinessService businessService;

    @PostMapping("/buy")
    @ApiOperation("执行业务的触发")
    @SentinelResource(value = "减少库存数",blockHandler = "blockError" )
    public JsonResult buy(){
        businessService.buy();
        return JsonResult.ok("购买完成!");
    }
    public JsonResult blockError(BlockException e){
        // 这个方法运行表示当前请求被限流了,我们给与返回,提示它被限流即可
        return JsonResult.failed(
        ResponseCode.INTERNAL_SERVER_ERROR,"服务器忙,请稍后再试");
    }
}
